package com.tom.architect.day17.simple1;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码多，不便于拓展。
 * eg：新增一种货箱，
 */
public class SplitService {

    // 把箱子进行拆分，多辆车来装.
    public static List<TruckCar> splitBox(IBox box) {
        List<TruckCar> carList = new ArrayList<>();
        while (box.getNumber() > 200) { // 要进行拆分
            // 判断箱子的种类。
            if (box instanceof PlasticClampBox) {
                PlasticClampBox orderBox = (PlasticClampBox) box; // 箱子强转
                PlasticClampBox newBox = new PlasticClampBox();
                newBox.setName(orderBox.getName()); // 属性设置。
                newBox.setNumber(200);

                TruckCar truckCar = new TruckCar();
                truckCar.addBox(newBox);
                carList.add(truckCar);
                box.setNumber(box.getNumber() - 200);
            } else if (box instanceof CarPartBox) {
                CarPartBox orderBox = (CarPartBox) box;
                CarPartBox newBox = new CarPartBox(); // 每辆车的箱子
                newBox.setName(orderBox.getName());
                newBox.setNumber(200);
                newBox.setCarBrand(orderBox.getCarBrand());

                TruckCar truckCar = new TruckCar();
                truckCar.addBox(newBox);
                carList.add(truckCar);
                box.setNumber(box.getNumber() - 200);
            }
        }

        // 最后不足200的箱子
        TruckCar truckCar = new TruckCar();
        truckCar.addBox(box);
        carList.add(truckCar);

        return carList;
    }


}
